package com.esms.struts.service;

import java.awt.AWTException;
import java.awt.Font;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.Locale;

import org.apache.log4j.Logger;

public class UIutil{
	private NumberFormat cnIntegerFormat;
	private NumberFormat cnDoubleFormat;	//2
	private NumberFormat doubleFormat0Digits;
	private NumberFormat doubleFormat3Digits;
	
	private static Logger log = Logger.getLogger(UIutil.class);
	
	private UIutil() {}
	public static UIutil $ = new UIutil(); 
	
	/*
	public static void startTrayIcon() {
	    final TrayIcon trayIcon;
	    final String appName = "ESMS";
	    final String mainPage = "http://localhost:8080/" + LocalContants.PROJECT_NAME;

	    if (SystemTray.isSupported()) {
	        SystemTray tray = SystemTray.getSystemTray();
	        Image image = Toolkit.getDefaultToolkit().getImage("web/images/esms.gif");		//d:/esms.gif

		MouseListener mouseListener = new MouseListener() {
		            
		    public void mouseClicked(MouseEvent e) {
		        log.info("Tray Icon - Mouse clicked!");                 
		}
		
		public void mouseEntered(MouseEvent e) {
			log.info("Tray Icon - Mouse entered!");                 
		}
		
		public void mouseExited(MouseEvent e) {
			log.info("Tray Icon - Mouse exited!");                 
		}
		
		public void mousePressed(MouseEvent e) {
			log.info("Tray Icon - Mouse pressed!");                 
		}
		
		public void mouseReleased(MouseEvent e) {
			log.info("Tray Icon - Mouse released!");                 
		    }
		};
		
		ActionListener exitListener = new ActionListener() {
		    public void actionPerformed(ActionEvent e) {
		    	log.info("Force Exit...");
		        System.exit(0);
		    }
		};
		
		ActionListener launchListener = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					launchFileWin32(mainPage);
				} catch (IOException ex) {
					ex.printStackTrace();
				}
			}
		};
		        
		PopupMenu popup = new PopupMenu();

		MenuItem launchMenu = new MenuItem("Launch ESMS main page");
		launchMenu.setFont(new Font(null).deriveFont(Font.BOLD));
		launchMenu.addActionListener(launchListener);
		
		MenuItem exitMenu = new MenuItem("Exit");
		exitMenu.addActionListener(exitListener);
		
		popup.add(launchMenu);
		popup.add(new MenuItem("-"));
		popup.add(exitMenu);
		
		trayIcon = new TrayIcon(image, appName, popup);
		
		ActionListener actionListener = new ActionListener() {
		    public void actionPerformed(ActionEvent e) {
		        trayIcon.displayMessage(appName, 
		        		appName + " is running!",
		            TrayIcon.MessageType.INFO);
		        
				try {
					launchFileWin32(mainPage);
				} catch (IOException ex) {
					ex.printStackTrace();
				}
		    }
		};
		        
		trayIcon.setImageAutoSize(true);
		trayIcon.addActionListener(actionListener);
		trayIcon.addMouseListener(mouseListener);
		
		try {
		    tray.add(trayIcon);
		} catch (AWTException e) {
			log.error("TrayIcon could not be added.");
		    }
		
		} else {
		
		    //  System Tray is not supported
		
	    }
	}
	
	public static void launchFileWin32(String url) throws IOException {
		Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
	}
	
	*/
	
	public String enc(boolean b) {
		return b ? "Yes" : "No";
	}

	public NumberFormat cnIntegerFormat() {
		if (cnIntegerFormat == null) {
			cnIntegerFormat  = NumberFormat.getInstance(Locale.CHINESE);
		}
		return cnIntegerFormat;
	}
	
	private NumberFormat cnDoubleFormat() {
		if (cnDoubleFormat == null) {
			cnDoubleFormat  = NumberFormat.getInstance(Locale.US);
			cnDoubleFormat.setMinimumFractionDigits(2);
			cnDoubleFormat.setMaximumFractionDigits(2);
		}
		return cnDoubleFormat;
	}
	
	private NumberFormat cnDouble0Format() {
		if (doubleFormat0Digits == null) {
			doubleFormat0Digits  = NumberFormat.getInstance(Locale.CHINESE);
			doubleFormat0Digits.setMinimumFractionDigits(0);
			doubleFormat0Digits.setMaximumFractionDigits(0);
		}
		return doubleFormat0Digits;
	}
	
	private NumberFormat cnDouble3Format() {
		if (doubleFormat3Digits == null) {
			doubleFormat3Digits  = NumberFormat.getInstance(Locale.CHINESE);
			doubleFormat3Digits.setMinimumFractionDigits(2);
			doubleFormat3Digits.setMaximumFractionDigits(3);
		}
		return doubleFormat3Digits;
	}
	
	public String enc(int d) {
		return cnIntegerFormat().format(d);
	}

	public String enc(long d) {
		return cnIntegerFormat().format(d);
	}

	public String enc(double d) {
		return cnDoubleFormat().format(d);
	}

	/**
	 * enc double with 3 digits fraction
	 * 
	 * @param d
	 * @return
	 */
	public String encDouble3(double d) {
		return cnDouble3Format().format(d); 
	}

	public String enc(String str) {
		return Strings.encodeHtml(str);
	}

	/**
	 *	Display the timestamp value of db data when you edit page 
	 */
	public String enc(java.sql.Timestamp time) {
		if (time == null)
			return "";
		String result = time.toString();
		int cutpoint = result.lastIndexOf('.');
		if (cutpoint >= 0)
			return result.substring(0, cutpoint);
		return null;
	}

	public String enc(Object obj) {
		if (obj instanceof Double)
			return enc(((Double) obj).doubleValue());

		if (obj instanceof Boolean)
			return enc(((Boolean) obj).booleanValue());

		return obj == null ? "" : Strings.encodeHtml(obj.toString());
	}
}
